﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
 
using System.Text;
using Com.Model.Cms;
using Com.Bll.Cms;
using Com.Common.Cms;
using System.Web.Mvc;
namespace Com.Controllers.Cms.Filters
{
    public class CmsController : Controller
    {
        #region//登录部分
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowLogon()
        {

            return null;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Logon()
        {

            return null;
        }
        #endregion

        #region//Cms后台首页部分
        public ActionResult Index()
        {
            return View("/Cms/Article/Index.cshtml");
        }
        #endregion

        #region//文章部分
        #region//Part 1.添加文章、文章内容
        /// <summary>
        /// 显示添加文章页面
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public ActionResult ShowAddArticle()
        {
            ArticleOperater helper = new ArticleOperater();

            #region//Step1.得到文章的所有类型
            List<Cms_ArticleType> ArticleTypes = helper.GetArticleTypes();
            Cms_ArticleType first = new Cms_ArticleType();
            first.ArticleTypeId = 0;
            first.ArticleTypeName = "- 请选择 -";
            ArticleTypes.Insert(0, first);
            ViewBag.ArticleType = new SelectList(ArticleTypes, "ArticleTypeId", "ArticleTypeName"); ;
            #endregion

            return View("/Views/Cms/Article/AddArticle.cshtml");
        }
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <returns></returns>
        [ValidateInput(false)]
        public void AddArticle(Cms_Article article, Cms_ArticleTitles articleTitles, Cms_ArticleSource articleSource, Cms_ArticleAuthor articleAuthor)
        {
            ArticleOperater helper = new ArticleOperater();
            List<String> Msg = new List<string>();

            StringBuilder js = new StringBuilder(32);

            #region//添加文章
            if (article != null)
            {
                #region//step1.1文章作者
                if (articleAuthor != null)
                {
                    if (!String.IsNullOrEmpty(articleAuthor.AuthorName))
                    {
                        articleAuthor.AuthorName = StringBoy.ClearHtml(articleAuthor.AuthorName.Trim());
                    }
                    else {
                        articleAuthor.AuthorName = String.Empty;
                    }
                        int authorId = helper.GetAuthorId(articleAuthor.AuthorName);
                    
                    if (authorId > 0)
                    {
                        article.AuthorId = authorId;
                    }
                    else
                    {
                        authorId = helper.InsertAuthor(articleAuthor);
                        if (authorId > 0)
                        {
                            article.AuthorId = authorId;
                        }
                        else
                        {
                            Msg.Add("增加文章作者失败");
                        }
                    }
                }
                else
                {
                    Msg.Add("文章作者为空");
                }
                #endregion

                #region//step1.2文章来源
                if (articleSource != null)
                {
                    if (!String.IsNullOrEmpty(articleSource.SourceName))
                    {
                        articleSource.SourceName = StringBoy.ClearHtml(articleSource.SourceName.Trim());
                    }
                    else
                    {
                        articleSource.SourceName = "-";
                    }
                    int sourceId = helper.GetArticleSourceId(articleSource.SourceName);
                    if (sourceId > 0)
                    {
                        article.SourceId = sourceId;
                    }
                    else
                    {
                        sourceId = helper.InsertArticleSource(articleSource.SourceName);
                        if (sourceId > 0)
                        {
                            article.SourceId = sourceId;
                        }
                        else
                        {
                            Msg.Add("增加文章来源失败");
                        }
                    }
                }
                else
                {
                    Msg.Add("文章来源为空");
                }
                #endregion

                #region//Step 1.3文章标题
                if (articleTitles != null)
                {
                    if (!String.IsNullOrEmpty(articleTitles.Title))
                    {
                        articleTitles.Title = StringBoy.ClearHtml(articleTitles.Title).Trim();
                        if (!String.IsNullOrWhiteSpace(articleTitles.SecondTitle))
                        {
                            articleTitles.SecondTitle = StringBoy.ClearHtml(articleTitles.SecondTitle.Trim());
                        }
                        if (!String.IsNullOrWhiteSpace(articleTitles.ThirdTitle))
                        {
                            articleTitles.ThirdTitle = StringBoy.ClearHtml(articleTitles.ThirdTitle.Trim());
                        }
                        if (!String.IsNullOrEmpty(articleTitles.Title))
                        {
                            int titleId = helper.InsertArticleTitle(articleTitles);
                            if (titleId > 0)
                            {
                                article.ArticleTitleId = titleId;
                            }
                            else
                            {
                                Msg.Add("添加文章标题失败");
                            }
                        }
                        else
                        {
                            Msg.Add("文章主标题为空");
                        }
                    }
                    else
                    {
                        Msg.Add("文章主标题为空");
                    }
                }
                #endregion

                #region//Step 1.4文章添加者
                article.CreateUserId = 1;
                #endregion

                #region//Step 1.5 添加文章
                if (Msg.Count < 1)
                {
                    int articleId = helper.InsertArticle(article);
                    if (articleId > 0)
                    {
                        js.Append("<script type='text/javascript'>")
                            .Append("alert('添加文章成功');")
                            .Append("window.location='")
                            .Append("/Cms/ShowAddArticleContent")
                            .Append("?ArticleId=" + articleId)
                            .Append("&PageIndex=1")
                            .Append("';")
                            .Append("</script>");

                        Response.Write(js);
                    }
                    else
                    {
                        js.Remove(0, js.Length);     
                        js.Append("<script type='text/javascript'>")
                           .Append("alert('添加文章失败');")
                           .Append("window.history.go(-1);")
                           .Append("</script>");

                        Response.Write(js);
                    }
                }
                else
                {
                    #region//输出消息
                    js.Remove(0, js.Length);
                    js.Append("<script type='text/javascript'>");
                    js.Append("alert");
                    js.Append("('");
                    foreach (String s in Msg)
                    {
                        js.Append(s);
                    }
                    js.Append("');");
                    Response.Write(js);
                    #endregion
                }
                #endregion
            }
            else
            {
                Msg.Add("文章实体为空");
                #region//输出消息
                js.Remove(0, js.Length);
                js.Append("<script type='text/javascript'>");
                js.Append("alert");
                js.Append("('");
                foreach (String s in Msg)
                {
                    js.Append(s);
                }
                js.Append("');");
                Response.Write(js);
                #endregion
            }
            #endregion
        }
        /// <summary>
        /// 添加文章内容
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowAddArticleContent()
        {
            StringBuilder js = new StringBuilder();
            int ArticleId = 0;
            int.TryParse(Request["ArticleId"], out ArticleId);
            int PageIndex = 0;
            int.TryParse(Request["PageIndex"], out PageIndex);
            if (ArticleId < 0)
            {
                js.Append("<script type='text/javascript'>")
                    .Append("alert('文章编号不正确');")
                    .Append("window.location='/Cms/Index';")
                    .Append("</script>");
            }
            else if (PageIndex < 1)
            {
                js.Append("<script type='text/javascript'>")
                                   .Append("alert('文章页号不正确');")
                                   .Append("window.location='/Cms/Index';")
                                   .Append("</script>");
            }
            else
            {
                ArticleOperater helper = new ArticleOperater();
                ViewBag.Title = helper.GetArticleTitlesByArticleId(ArticleId).Title;
                ViewBag.ArticleId = ArticleId;
                ViewBag.PageIndex = PageIndex;
            }
            Response.Write(js.ToString());
            return View("/Views/Cms/Article/AddArticleContent.cshtml");
        }

        [ValidateInput(false)]
        public void InsertArticleContent(Cms_ArticleContent content)
        {
            StringBuilder js = new StringBuilder(32);
            try
            {
                ArticleOperater helper = new ArticleOperater();
                int id = helper.InsertArticleContent(content);
                js.Append("<script type=\"text/javascript\">")
                    .Append("alert('添加文章成功');")
                    .Append("window.location='/Cms/ShowAddArticleContent?ArticleId=" + content.ArticleId + "&PageIndex=" + (content.PageIndex + 1) + "';")
                    .Append("</script>");
            }
            catch
            {
                js.Append("<script type=\"text/javascript\">")
                    .Append("alert('添加文章时发生异常');")
                    .Append("window.location='/Cms/ShowAddArticleContent?ArticleId=" + content.ArticleId + "&PageIndex=" + content.PageIndex + "';")
                    .Append("</script>");
            }
            Response.Write(js.ToString());
        }
        #endregion

        #region//Part 2.修改文章、文章内容

        #endregion

        #region//Part 3.文章列表
        public ActionResult  ArticleList()
        {
            #region
           
            ArticleOperater helper = new ArticleOperater();

            #region//构造查询条件

            Expression<Func<V_ArticleDetail, bool>> conditions = PredicateExtension.True<V_ArticleDetail>();

            #region//页码
            int pageIndex = 1;
            int pageSize = 10;
            if (!int.TryParse(Request["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }
            #endregion

            #region//标题
            String title = Request["ArticleTitle"];
            if (!String.IsNullOrEmpty(title))
            {
                title = title.Trim();
                if (!String.IsNullOrEmpty(title))
                {
                    conditions = conditions.And(item=>item.Title.Contains(title));
                }
            }
            #endregion

            #region//作者
            int authorId = 0;
            if (int.TryParse(Request["AuthorId"], out authorId))
            {
                conditions = conditions.And(item => item.ArticleId == authorId);
            }
            #endregion

            #region//状态
            int articleStateId = 0;
            if (int.TryParse(Request["ArticleStateId"], out articleStateId))
            {
                if (articleStateId > 0)
                {
                    conditions = conditions.And(item => item.ArticleStateId == articleStateId);
                }
            }
            #endregion

            #region//文章类型
            int articleType = 0;
            if (int.TryParse(Request["ArticleTypeId"], out articleType))
            {
                if (articleType > 0)
                {
                    conditions = conditions.And(item => item.ArticleTypeId == articleType);
                }
            }
            #endregion
            #endregion

            #region//数据列表
            Expression<Func<V_ArticleDetail,Int32>> orderBy = item=>item.ArticleId;
            List<V_ArticleDetail> lst = helper.GetArticles(pageIndex, pageSize, conditions, orderBy);
            #endregion

            #region//页码
            LinkedList<PageNumberModel> pages = new LinkedList<PageNumberModel>();
            int recordCount = helper.GetArticleCount(conditions);
            pages = PageBuilder.LinkedPage(recordCount, pageSize, pageIndex, 10, "go({page});", "{page}");
            #endregion

            #region//序列化
            Dictionary<String, Object> result = new Dictionary<string, object>();
            result.Add("list", lst);
            result.Add("pages", pages);
            JsonResult jr = new JsonResult();
            jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            jr.Data = result;
            return jr;
            #endregion
          
            #endregion
        }
        #endregion
        #endregion
    }
}
